/*
 * @(#)RepositoryClient.java
 * Date 2013-04-11
 * Version 1.0
 * Author Jim Horner
 * Copyright (c)2012
 */


package us.hornerscorners.lamppost.xca.client;

import javax.enterprise.context.ApplicationScoped;

import javax.xml.ws.BindingProvider;

import javax.xml.ws.soap.AddressingFeature;

import ihe.iti.xds_b._2007.RespondingGatewayRetrievePortType;
import ihe.iti.xds_b._2007.RespondingGatewayRetrieveService;
import ihe.iti.xds_b._2007.RetrieveDocumentSetRequestType;
import ihe.iti.xds_b._2007.RetrieveDocumentSetResponseType;

import us.hornerscorners.lamppost.config.EndpointConfig;

import us.hornerscorners.lamppost.model.audit.AuditContext;

import us.hornerscorners.lamppost.repository.client.IRepositoryClient;

import us.hornerscorners.lamppost.repository.service.AtnaRepositoryService;

import us.hornerscorners.lamppost.ws.AbstractWSClient;
import us.hornerscorners.lamppost.ws.AddressCallback;


/**
 * Class description
 *
 *
 * @version        v1.0, 2013-04-11
 * @author         Jim Horner
 */
@ApplicationScoped
public class RespondingRetrieveClient extends AbstractWSClient
        implements IRepositoryClient {

    /** Field description */
    private final AtnaRepositoryService atnaService;

    /** Field description */
    private final String clientId;

    /** Field description */
    private final RespondingGatewayRetrieveService repositoryService;

    /**
     * Constructs ...
     *
     *
     * @param atnaService
     * @param clientId
     * @param cfg
     */
    public RespondingRetrieveClient(AtnaRepositoryService atnaService, String clientId,
                                    EndpointConfig cfg) {

        this(atnaService, new RespondingGatewayRetrieveService(), clientId, cfg);
    }

    /**
     * Constructs ...
     *
     *
     * @param atnaService
     * @param repositoryService
     * @param clientId
     * @param cfg
     */
    public RespondingRetrieveClient(AtnaRepositoryService atnaService,
                                    RespondingGatewayRetrieveService repositoryService,
                                    String clientId, EndpointConfig cfg) {

        super(cfg);
        this.repositoryService = repositoryService;
        this.clientId = clientId;
        this.atnaService = atnaService;
    }

    /**
     * Method description
     *
     *
     * @return
     */
    @Override
    public String getClientId() {

        return this.clientId;
    }

    /**
     * Method description
     *
     *
     * @param request
     *
     * @return
     */
    @Override
    public RetrieveDocumentSetResponseType retrieveDocument(AuditContext auditContext,
            RetrieveDocumentSetRequestType request) {

        RetrieveDocumentSetResponseType result = null;

        RespondingGatewayRetrievePortType port =
            this.repositoryService.getRespondingGatewayRetrievePortSoap12(
                new AddressingFeature(true, true));

        AddressCallback addressCallback = configureBindingProvider(auditContext,
                                              (BindingProvider) port);

        result = port.respondingGatewayCrossGatewayRetrieve(request);

        AuditContext outgoingContext = parseOutgoingContext(auditContext,
                                           addressCallback);

        this.atnaService.sendRetrieveRequestAuditMessage(outgoingContext, result);

        return result;
    }
}
